//
//  SplitRootViewController.m
//  Gabber
//
//  Created by 惠国 陆 on 11-11-15.
//  Copyright (c) 2011年 恒生聚源. All rights reserved.
//

#import "SplitRootViewController.h"

#import "RosterViewController.h"
#import "ChatViewController.h"
#import "AboutViewController.h"
#import "SettingsViewController.h"
#import "MessagesViewController.h"
#import "CardViewController.h"
#import "MainViewController.h"

@interface SplitRootViewController()

- (void)setupURLRouting;

@end

@implementation SplitRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setupURLRouting];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [super viewWillAppear:animated];
    
    [self.leftNavigator openURLs:@"gabber://roster", nil];
    [self.rightNavigator openURLs:@"gabber://main", nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];  
    if (version >= 5.0)  
    {  
        return YES; 
    }else{
        return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;  
    }
    

    
    
}

- (id)willOpenUrlPath:(NSURL*)url {
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [self.rightNavigator openURLAction:[TTURLAction actionWithURLPath:[url absoluteString]]];
    
    [self.popoverSplitController dismissPopoverAnimated:YES];
    
    // We need to do this because the right navigator clobbered the right navigation controller
    // and our button along with it.
    [self updateSplitViewButton];
    
    // Don't create a view controller here; we're forwarding the URL routing.
    return nil;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)routeLeftNavigator {
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    TTURLMap* map = self.leftNavigator.URLMap;
  
    // Forward all unhandled URL actions to the right navigator.
    [map                    from: @"*"
                        toObject: self
                        selector: @selector(willOpenUrlPath:)];
    
    [map                    from: @"gabber://roster"
                toViewController: [RosterViewController class]];
    [map from:@"gabber://vcard/(initWithJidStr:)" toViewController:[CardViewController class]];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)routeRightNavigator {
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    TTURLMap* map = self.rightNavigator.URLMap;

    [map from: @"*" toViewController: [TTWebController class]];
    [map from:@"gabber://main" toViewController:[MainViewController class]];  
//    [map from:@"gabber://settings" toViewController:[SettingsViewController class]];   
//    [map from:@"gabber://messages" toViewController:[MessagesViewController class]];   
//    [map from:@"gabber://chat/(initWithJidStr:)" toViewController:[ChatViewController class]];

}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setupURLRouting {
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); 
    [self routeLeftNavigator];
    [self routeRightNavigator];
}

- (BOOL)splitViewController: (UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
{
    return NO;
}

@end
